package com.health.platform.common.sys.user.service;

import com.alibaba.fastjson.JSONObject;
import com.health.platform.common.http.HttpClientUtils;
import com.health.platform.common.restful.AppResponse;
import com.health.platform.common.sys.login.entity.UserEntity;
import com.health.platform.common.sys.user.entity.UserInfo;
import com.health.platform.common.utils.RedisUtil;
import com.health.platform.common.utils.ThreadLocalUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>用户信息Service</p>
 * <p>创建日期：2021-02-07</p>
 *
 * @author zhou-qf
 */
@Service
@Data
public class UserService {

    @Value("${auth.getUserUrl}")
    private String getUserUrl;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取登录用户信息
     *
     * @return 用户信息结果
     */
    public AppResponse getUserInfo(){
        UserEntity userEntity = ThreadLocalUtil.get();
        String token = userEntity.getToken();
        HashMap param = new HashMap(3){
            {
                put("token", token);
                put("type", "1");
                put("systemid", "DataManagementSystem");
            }
        };
        // 调用远程登录接口
        String result = HttpClientUtils.httpPostRequest(getUserUrl, param).getContent();
        // 转换实体
        UserInfo userInfo = JSONObject.parseObject(result, UserInfo.class);
        // 返回结果
        if(userInfo.isSuccess()){
            // 缓存用户具体信息
            userEntity.setBody(userInfo.getBody());
            redisUtil.set(userEntity.getLocalToken(), userEntity);
            return AppResponse.success("获取信息成功", userInfo.getBody());
        } else {
            return AppResponse.bizError("获取信息失败");
        }
    }

}
